package com.example.chatroom.service;

import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.HashSet;

@Service
public class ChatService {
    private Map<String, String> userSessions = new ConcurrentHashMap<>();
    
    public void addUser(String sessionId, String username) {
        userSessions.put(sessionId, username);
    }
    
    public void removeUser(String sessionId) {
        userSessions.remove(sessionId);
    }
    
    public Set<String> getOnlineUsers() {
        return new HashSet<>(userSessions.values());
    }
    
    public String getUser(String sessionId) {
        return userSessions.get(sessionId);
    }
    
    public void updateUsername(String sessionId, String newUsername) {
        String oldUsername = userSessions.get(sessionId);
        if (oldUsername != null) {
            userSessions.put(sessionId, newUsername);
        }
    }
} 